﻿using CloudStorageLight.Core;
using CloudStorageLight.Core.Web;
using CloudStorageLight.SharePointWeb.Models;
using Microsoft.AspNet.Identity;
using Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.Azure.ActiveDirectory.GraphClient.Extensions;
using Microsoft.Owin.Security;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using DropNet;
using System.Threading;

namespace CloudStorageLight.SharePointWeb.Controllers
{
    public class SettingsController : SettingsControllerBase
    {
        protected override BlobService CreateBlobService()
        {
            return new BlobService();
        }

        protected override BlobUser GetCurrentUser()
        {
            if (_user == null)
            {
                _user = BasicAuthenticateUtil.GetCurrentUser();
            }
            return _user;
        }
        private BlobUser _user;

        public override async Task<ActionResult> ConfigurationEdit(string submitButton, BlobConfigurationItem vm)
        {
            SetupSharePointListItem(vm);
            if (submitButton != "Update")
            {
                return await base.ConfigurationEdit(submitButton, vm);
            }
            else
            {
                return View(vm);
            }
        }

        public ActionResult SharePointListItem(string spSiteUrl, string term)
        {
            var items = ListSharePointList(spSiteUrl);
            term = term.Trim();
            var filteredItems = items.Where(
                item => item.Value.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
                ).Select(x => x.Value);
            return Json(filteredItems, JsonRequestBehavior.AllowGet);
        }



        private void SetupSharePointListItem(BlobConfigurationItem vm)
        {
            if (string.IsNullOrEmpty(vm.SpSiteUrl)) vm.SpSiteUrl = Request.QueryString["SPHostUrl"];
            var documentList = ListSharePointList(vm.SpSiteUrl);
            ViewData["documentList"] = documentList;
            documentList.Insert(0, new SelectListItem());
        }

        private List<SelectListItem> ListSharePointList(string siteUrl)
        {
            var documentList = new List<SelectListItem>();
            var client = AttachedSharePointBlobAdapter.GetSpsClientContext();
            if (client != null)
            {
                //var spCtx = new ClientContext(siteUrl);
                //spCtx.Credentials = client.Credentials;
                var spCtx = client;

                var web = spCtx.Web;
                spCtx.Load(web);
                spCtx.ExecuteQuery();

                var lists = web.Lists;
                spCtx.Load(lists, x => x.Include(
                    y => y.IsApplicationList,
                    y => y.Title,
                    y => y.Hidden,
                    y => y.BaseType,
                    y => y.RootFolder.Name));

                spCtx.ExecuteQuery();
                documentList = lists
                    .Where(x => !x.IsApplicationList && !x.Hidden && x.Title != "Form Templates" && x.Title != "Customized Reports" && x.Title != "Site Collection Documents" && x.Title != "Site Collection Images" && x.Title != "Images" && x.BaseType == Microsoft.SharePoint.Client.BaseType.DocumentLibrary)
                    .Select(x => new SelectListItem { Text = x.Title, Value = x.RootFolder.Name }).ToList();

            }
            return documentList;
        }

    }
}